package com.leiqichao.backend.controller;

import com.leiqichao.backend.common.Page;
import com.leiqichao.backend.common.Result;
import com.leiqichao.backend.entity.Article;
import com.leiqichao.backend.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/api/resume")
public class ResumeController {

    @GetMapping("pdf")
    public ResponseEntity<Resource> getPdf() {
        try {
            // 从 classpath 的 resources 目录读取文件
            ClassPathResource pdfFile = new ClassPathResource("jianli.pdf"); // 修改为你的文件路径

            // 将输入流转换为字节数组（更可靠的资源处理方式）
            InputStream inputStream = pdfFile.getInputStream();
            byte[] pdfBytes = StreamUtils.copyToByteArray(inputStream);

            // 使用 ByteArrayResource 代替 InputStreamResource（避免流关闭问题）
            ByteArrayResource resource = new ByteArrayResource(pdfBytes);

            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_TYPE, "application/pdf")
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=display.pdf")
                    .body(resource);

        } catch (IOException e) {
            // 处理文件不存在的情况
            return ResponseEntity.notFound().build();
        }
    }
}